package com.yeyks.commonReference.append.mongodb.service.impl;

import com.yeyks.common.param.PageParam;
import com.yeyks.commonReference.append.mongodb.entity.CouponPushMongoEntity;
import com.yeyks.commonReference.append.mongodb.repository.CouponPushUserMongoRepository;
import com.yeyks.commonReference.append.mongodb.service.CouponPushUserMongoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author: dagny
 * @date: 2019/11/28 0028 13:12
 */
@Service
@Slf4j
public class CouponPushUserMongoServiceImpl implements CouponPushUserMongoService {

    @Autowired
    private CouponPushUserMongoRepository couponPushUserMongoRepository;

    @Override
    public boolean saveBatch(List<CouponPushMongoEntity> couponPushMongoEntityList) {

        if(!CollectionUtils.isEmpty(couponPushMongoEntityList)){
            try {
                List<String> ids = couponPushMongoEntityList.stream().map(CouponPushMongoEntity::getUserId).collect(Collectors.toList());
                Iterable<CouponPushMongoEntity> allById = couponPushUserMongoRepository.findAllById(ids);
                ids.clear();
                for (CouponPushMongoEntity pushMongoEntity : allById) {
                    ids.add(pushMongoEntity.getUserId());
                }
                couponPushMongoEntityList.removeIf(couponPushMongoEntity -> ids.contains(couponPushMongoEntity.getUserId()));

                couponPushUserMongoRepository.insert(couponPushMongoEntityList);
            }catch (Exception e){
                log.info("重复数据过滤：{}",e.getMessage());
            }
            return true;
        }
        log.info("插入数据为空!");
        return false;
    }

    @Override
    public boolean clearCouponPush() {

        couponPushUserMongoRepository.deleteAll();

        return true;
    }

    @Override
    public CouponPushMongoEntity get(Integer id) {

        Optional<CouponPushMongoEntity> byId = couponPushUserMongoRepository.findById(String.valueOf(id));
        if(byId.isPresent()){
            return byId.get();
        }
        return null;
    }

    @Override
    public List<CouponPushMongoEntity> queryByPage(PageParam pageParam) {

        Sort sort = new Sort(Sort.Direction.DESC, "_id");
        int pageNum = pageParam.getPageNum();
        if(pageNum > 0){
            pageNum = pageNum -1;
        }
        PageRequest pageRequest = PageRequest.of(pageNum, pageParam.getPageSize(),sort);
        Page<CouponPushMongoEntity> all = couponPushUserMongoRepository.findAll(pageRequest);
        if(all.getSize() > 0){
            return all.getContent();
        }
        return null;
    }

    @Override
    public Long queryCount(PageParam pageParam) {
        return couponPushUserMongoRepository.count();
    }

    @Override
    public List<CouponPushMongoEntity> findAll() {
        return couponPushUserMongoRepository.findAll();
    }

    @Override
    public boolean save(CouponPushMongoEntity entity) {

        try {
            couponPushUserMongoRepository.save(entity);
            return true;
        }catch (Exception e){
            log.info("请勿重复添加数据");
        }
        return false;
    }
}
